//
//  FirstScrollView.m
//  Fragrance
//
//  Created by yinzhongyuan on 15/5/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "FirstScrollView.h"

@implementation FirstScrollView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createScrollView];
    }
    return self;
}

- (void)createScrollView
{
    self.myScroll = [[UIScrollView alloc] init];
    [self addSubview:self.myScroll];
    [self.myScroll release];
    
    
    self.myPage = [[UIPageControl alloc] init];
    [self addSubview:self.myPage];
    [self.myPage release];
}


-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.myScroll.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self.myScroll setContentSize:CGSizeMake(self.frame.size.width * 3, self.frame.size.height)];
    [self.myScroll setContentOffset:CGPointMake(0, 0)];
    self.myScroll.bounces = NO;
    self.myScroll.pagingEnabled = YES;
    self.myScroll.showsHorizontalScrollIndicator = NO;
    self.myScroll.showsVerticalScrollIndicator = NO;
    self.myScroll.delegate = self;
    
    
    
    for (int i = 0; i < 3; i++) {
        HomeDataModel *model = [self.dataArr objectAtIndex:i];
       
        //添加图片
        self.picture = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height)];
        NSString *str = model.iconName;
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:str]];
        UIImage *image = [UIImage imageWithData:data];
        self.picture.image = image;
        [self.myScroll addSubview:self.picture];
        
        //添加标题
        self.title = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width * i , self.frame.size.height - 25, self.frame.size.width, 20)];
        self.title.text = model.caiTitle;
        self.title.textColor = [UIColor blackColor];
        NSLog(@"%@", model.caiTitle);
        self.title.textColor = [UIColor grayColor];
        [self.myScroll addSubview:self.title];
        self.title.alpha = 0.5;
        self.title.backgroundColor = [UIColor whiteColor];
        [self.title release];
        [self.picture release];
        
    }
    
    self.myPage.frame = CGRectMake(self.frame.size.width - 100, self.frame.size.height - 10, 80, 0);
    self.myPage.numberOfPages = 3;
    self.myPage.currentPage = 0;
    self.myPage.currentPageIndicatorTintColor = [UIColor orangeColor];
    self.myPage.pageIndicatorTintColor = [UIColor grayColor];
}


//实现scrollview的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    
    NSInteger i = self.myScroll.contentOffset.x / self.frame.size.width;
    self.myPage.currentPage = i;
    
}







/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
